package cn.fay.chapter15;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class Apply {
	public static <T,S extends Iterable<? extends T>> void apply(S seq,Method f,Object... args){
		try {
			for (T t : seq) {
				f.invoke(t, args);
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
	}
	public static void main(String[] args) throws Exception {
		List<Shape> shapes=new ArrayList<Shape>();
		for (int i = 0; i < 10; i++) {
			shapes.add(new Shape());
		}
		apply(shapes, Shape.class.getMethod("rotate"));
		apply(shapes, Shape.class.getMethod("resize", int.class), 5);
		List<Square> squares=new ArrayList<Square>();
		for(int i=0;i<10;i++){
			squares.add(new Square());
		}
		apply(squares, Square.class.getMethod("rotate"));
		apply(squares, Square.class.getMethod("resize",int.class),5);
	}
}
class Shape{
	private static long counter=0;
	private final long id=counter++;
	public void rotate() {
		System.out.println(this+" rotate");
	}
	public void resize(int newsize) {
		System.out.println(this+" resize "+newsize);
	}
	@Override
	public String toString() {
		return this.getClass().getSimpleName()+id+" ";
	}
}
class Square extends Shape{}

class FilledList<T> extends ArrayList<T>{
	public FilledList(Class<? extends T> type,int size) {
		try {
			for (int i = 0; i < size; i++) {
				add(type.newInstance());
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
	}
}

